﻿$(function () {
    ko.applyBindings(vm, $('#collapseTwo')[0]);
    ko.applyBindings(vm, $('#leftcol')[0]);

    initUI();
    elementDrag();
});

var vm = {
    TypeObser: ko.observable(),
    AppTypes: ko.observableArray(),
    DomainTypes: ko.observableArray(),
    AllTypes: ko.observableArray(),
    delAppType: function (element) {
        //Manual delete from foreign key
        var delCon1 = new breeze.EntityQuery().from("OutsideRelations").where("AppId", "==", element.UniqueId());
        var delEnt1 = manager.executeQueryLocally(delCon1);
        for (var i = 0; i < delEnt1.length; i++) {
            delEnt1[i].entityAspect.setDeleted();
        }

        var delCon2 = new breeze.EntityQuery().from("AppRelations").where("AppParId", "==", element.UniqueId());
        var delEnt2 = manager.executeQueryLocally(delCon2);
        for (var i = 0; i < delEnt2.length; i++) {
            delEnt2[i].entityAspect.setDeleted();
        }
        var delCon3 = new breeze.EntityQuery().from("AppRelations").where("AppChildId", "==", element.UniqueId());
        var delEnt3 = manager.executeQueryLocally(delCon3);
        for (var i = 0; i < delEnt3.length; i++) {
            delEnt3[i].entityAspect.setDeleted();
        }

        element.entityAspect.setDeleted();
        vm.AppTypes.remove(element);
        //vm.AllTypes.remove(function (item) { return item.UniqueId == ("AppId" + element.UniqueId()) });
        vm.AllTypes.remove(function (item) { return item.UniqueId == element.UniqueId() });
        Save();
    },

    AdvanceCom: ko.observableArray(),
    SelectedLayout: ko.observable(),
}

vm.SelectedLayout.subscribe(function (id) {
    if (id != '' && id != undefined) {
        clearLayout();
        var html = getHtml(_getLayout(id));
        $('#contain').append(html);
    }
});

var manager = new breeze.EntityManager('Breeze/Data');


var query = new breeze.EntityQuery().from("AppTypes");
manager.executeQuery(query).then(function (data) {
    data.results.forEach(function (item) {
        vm.AppTypes.push(item);
        //var tmp = new Object;
        //tmp.UniqueId = "AppId" + item.UniqueId();
        //tmp.UniqueId = item.UniqueId();
        //tmp.ObjName = item.ObjName();
        vm.AllTypes.push(item);
    });
}).fail(function (e) {
    alert(e);
});
var query2 = new breeze.EntityQuery().from("DomainTypes");
manager.executeQuery(query2).then(function (data) {
    data.results.forEach(function (item) {
        vm.DomainTypes.push(item);
        //var tmp = new Object;
        //tmp.UniqueId = "DomId" + item.UniqueId();
        //tmp.UniqueId = item.UniqueId();
        //tmp.ObjName = item.ObjName();
        vm.AllTypes.push(item);
        $('#_loading').hide();
        $($('#_loading')[0].nextElementSibling).show();
    });
}).fail(function (e) {
    alert(e);
});

function Save() {
    if (manager.hasChanges()) {
        manager.saveChanges().then(saveSucceeded).fail(saveFailed);
    } else {
        alert("Nothing to save");
    };
}
function saveSucceeded(data) {
    alert("Successful");
}
function saveFailed(err) {
    alert(err);
}



ko.bindingHandlers.draggable = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        $(element).draggable({
            helper: 'clone'
        });
    }
};

ko.bindingHandlers.droppable = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        $(element).droppable();
    }
};